package project.dade.cn.bst;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;

import cn.bingoogolapple.bgabanner.BGABanner;
import project.dade.cn.bst.activity.LoginActivity;
import project.dade.cn.bst.base.AbsFragmentActivity;
import project.dade.cn.bst.base.IPage;
import project.dade.cn.bst.base.ParentFragment;
import project.dade.cn.bst.fragments.IndexFragment;
import project.dade.cn.bst.fragments.InvestmentFragment;
import project.dade.cn.bst.fragments.MyFragment;
import project.dade.cn.bst.fragments.ShoppingFragment;
import project.dade.cn.bst.manager.AppApplication;
import project.dade.cn.bst.utils.Util;


public class MainActivity extends AbsFragmentActivity implements IPage
       {
    //首页
    private IndexFragment indexFragment;
    //购物车
    private ShoppingFragment shoppingFragment;
    //我的
    private MyFragment myFragment;
    //更多
    private InvestmentFragment investmentFragment;
    //radioButton的父组件
    private RadioGroup rg_home_bottom = null;
    //记录radioButton的下标
    private int currIndex;

           private RadioButton foot_bar_index;
           public static MainActivity instance = null;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        rg_home_bottom = (RadioGroup) findViewById(R.id.rg_home_bottom);
        rg_home_bottom.setOnCheckedChangeListener(new MyRadioCheckedListener());
        currIndex = R.id.foot_bar_index;
        foot_bar_index= (RadioButton) findViewById(R.id.foot_bar_index);
//        Util.addActivity(this);
        instance=this;
    }


           @Override
           public void putFragments(ArrayList<ParentFragment> fragments) {
               initFrags();
               fragments.add(indexFragment);
               fragments.add(shoppingFragment);
               fragments.add(myFragment);
               fragments.add(investmentFragment);
           }

           @Override
           public int getDefaultIndex() {
               return 0;
           }

           @Override
           public int getHolderID() {
               return R.id.lay_content_container;
           }
           /**
            * 初始化选项
            */
           public void initOption() {
               switchPage(0);
               currIndex =  R.id.foot_bar_index;
               foot_bar_index.setChecked(true);
           }

    @Override
    public IPage getIpage() {
        return this;
    }

           @Override
           protected void onDestroy() {
//               Util.  removeActivity(this);
               super.onDestroy();
           }

    @Override
    public FragmentActivity getCurrentActivity() {
        return this;
    }

    @Override
    public int getRootViewID() {
        return R.layout.acticity_bottom;
    }
           /***
            *
            * 初始化所有页面
            *
            */
           private void initFrags(){
              indexFragment = new IndexFragment();
               shoppingFragment = new ShoppingFragment();
              myFragment = new MyFragment();
             investmentFragment = new InvestmentFragment();
           }
           private class MyRadioCheckedListener implements android.widget.RadioGroup.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(RadioGroup arg0, int checkedId) {

            // 说明当前点击的仍然是之前的不再重复执行执行
            if(currIndex == checkedId){
                return;
            }

            switch(checkedId){
                case R.id.foot_bar_index:
                        switchPage(0);
                        currIndex = checkedId;
                    break;

                case R.id.foot_bar_shoping:
                    if (AppApplication.getInstance().getSpUtil().isLogin()) {
                        switchPage(1);
                        currIndex = checkedId;
                    }else {
                        foot_bar_index.setChecked(true);
                        startActivity(new Intent(MainActivity.this, LoginActivity.class));
                    }
                    break;

                case R.id.foot_bar_my:
                    if (AppApplication.getInstance().getSpUtil().isLogin()) {
                        switchPage(2);
                        currIndex = checkedId;
                    }else {
                        foot_bar_index.setChecked(true);
                        startActivity(new Intent(MainActivity.this, LoginActivity.class));
                    }
                    break;

                case R.id.main_footbar_more:
                    switchPage(3);
                    currIndex = checkedId;
                    break;
            }
        }
    }


       }
